/**
 *
 */
package org.text2diagram.nlp.opennlp;

import java.util.Arrays;

import jsr166y.ForkJoinPool;

import org.text2diagram.nlp.core.NLPException;
import org.text2diagram.nlp.core.NLPProcessor;
import org.text2diagram.nlp.model.CASData;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import extra166y.Ops;
import extra166y.ParallelArray;

/**
 * @author alex
 *
 */
public class OpenNLPProcessor implements NLPProcessor<CASData> {

	/**
	 * the ForkJoin pool of treads.
	 */
	private ForkJoinPool fjPool;

	@Override
	public CASData createCASData() {
		return new CASData() {

			/**
			 * CAS data.
			 */
			private JsonObject data = new JsonObject();

			@Override
			public String toPOSString() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public JsonObject getCASRoot() {
				return data;
			}

			@Override
			public JsonElement getCASElement(String key) {
				return data.get(key);
			}

		};
	}

	@Override
	public void process(final CASData aCASData) throws NLPException {
		System.out.println(aCASData.getCASElement("Text"));
		String[] sentences = OpenNLPSentenceDetector.getInstance().detect(aCASData.getCASElement("Text").getAsString());
		Gson gson = new Gson();
		aCASData.getCASRoot().add("Sentences", gson.toJsonTree(sentences));

		//ParallelArray<String> processor = ParallelArray.createUsingHandoff(sentences, fjPool);
		//processor.apply(tokeniser);

		//TODO : model to JsonElement or google libraries
		String[] tokens;
		String[] posTags;
		String[] chanks;
		for (JsonElement sentence : aCASData.getCASElement("Sentences").getAsJsonArray()) {
			tokens = OpenNLPTokenDetector.getInstance().detect(sentence.getAsString());
			System.out.println(Arrays.asList(tokens));
			posTags = OpenNLPPosTagDetector.getInstance().detect(tokens);
			System.out.println(Arrays.asList(posTags));
			chanks = OpenNLPChunkDetector.getInstance().detect(tokens, posTags);
			System.out.println(Arrays.asList(chanks));

		}

		// TODO Auto-generated method stub

	}

	private static final Ops.Op<String, String[]> tokenizer = new Ops.Op<String, String[]>() {

		@Override
		public String[] op(String arg0) {
			// TODO Auto-generated method stub
			return null;
		}

	};

}
